﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMLGener.Utils
{
    /// <summary>
    /// Delegate error loging event
    /// </summary>
    /// <param name="message"></param>
    public delegate void XErrorEvent(XLogMessage message);
   
    /// <summary>
    /// Delegate loging event
    /// </summary>
    /// <param name="message"></param>
    public delegate void XLogEvent(XLogMessage message);

    /// <summary>
    /// 
    /// </summary>
    public class XLogger
    {
        /// <summary>
        /// List of log messages
        /// </summary>
        public Stack<XLogMessage> list = new Stack<XLogMessage>();
        
        /// <summary>
        /// Public (default) logger instance.
        /// </summary>
        public static XLogger instance = new XLogger();
        
        /// <summary>
        /// Event, which is disposed, if some error log was called.
        /// </summary>
        public event XErrorEvent errorEvent;
        
        /// <summary>
        /// Event, which is disposed, if some log was called.
        /// </summary>
        public event XLogEvent logEvent;

        /// <summary>
        /// General calling of adding message to log list.
        /// </summary>
        /// <param name="source">Message sender</param>
        /// <param name="message">Message text to be added to log</param>
        public static void add(string source,string message)
        {
            instance.addLog(source,message, XLogMessage.LogType.LOG);
        }

        /// <summary>
        /// Add standard log.
        /// </summary>
        /// <param name="source">Message sender</param>
        /// <param name="message">Message text to be added to log</param>
        public void addLog(string source, string message)
        {
            this.addLog(source, message, XLogMessage.LogType.LOG);
        }

        /// <summary>
        /// General calling of adding message to log list.
        /// </summary>
        /// <param name="source">Message sender</param>
        /// <param name="message">Message text to be added to log</param>
        /// <param name="type">Type of message : error log/log</param>
        public static void add(string source, string message, XLogMessage.LogType type)
        {
            instance.addLog(source, message, type);
        }

        /// <summary>
        /// General calling of adding error message to log list
        /// </summary>
        /// <param name="source">Message sender</param>
        /// <param name="message">Error message text to be added to log</param>
        public static void addError(string source, string message)
        {
            instance.addLog(source,message, XLogMessage.LogType.ERROR);
        }

        /// <summary>
        /// Add error log.
        /// </summary>
        /// <param name="source">Message sender</param>
        /// <param name="message">Error message text to be added to log</param>
        public void addErrorLog(string source, string message)
        {
            this.addLog(source, message, XLogMessage.LogType.ERROR);
        }

        /// <summary>
        /// Add error log.
        /// </summary>
        /// <param name="source">Message sender</param>
        /// <param name="message">Error message text to be added to log</param>
        public void addErrorHintLog(string source, string message)
        {
            this.addLog(source, message, XLogMessage.LogType.ERROR_HINT);
        }

        /// <summary>
        /// Add log/error log message to list.
        /// </summary>
        /// <param name="source">Message sender</param>
        /// <param name="message">Message text</param>
        /// <param name="type">Indicator, if message is log or error</param>
        public void addLog(string source,string message, XLogMessage.LogType type)
        {
            XLogMessage xmess = new XLogMessage(source,message, type);
            this.list.Push(xmess);
            if (errorEvent != null && (type == XLogMessage.LogType.ERROR || type == XLogMessage.LogType.ERROR_HINT
                || type == XLogMessage.LogType.WARNING ))
                errorEvent(xmess);
            if ((type != XLogMessage.LogType.ERROR && type != XLogMessage.LogType.ERROR_HINT && type != XLogMessage.LogType.WARNING) && logEvent != null)
                logEvent(xmess);
        }


        

    }
}
